package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author morgan
	 */
	public class SelectableObject extends FlxSprite
	{
		// 0-1 how selected it is!
		public var selectedValue:Number = 0;
		// How close the cursor needs to be to start selection
		public var selectRadius:Number = 0;
		// Scale based on selection
		public var baseScale:Number = 0.9;
		public var maxScale:Number = 1.0;
		
		// Set to false to disable stuff.
		public var selectable:Boolean = true;
		
		// How long it has been selected, in total
		public var selectedTime:Number = 0;
		
		public var dx:Number = 0;
		public var dy:Number = 0;
		public var dist:Number = 1000;
		
		public function SelectableObject(x:int, y:int, image:Class = null) 
		{
			super(x, y, image);
			
			selectRadius = (width + height) / 2;
		}
		
		override public function update():void 
		{
			super.update();
			
			if (selectable)
			{
				updateSelection();
			}
			else
			{
				alpha = 0.8;
				scale.x = scale.y = baseScale;
				selectedValue = Math.max(selectedValue - FlxG.elapsed, 0);
			}
		}
		
		public function updateSelection():void
		{
			// Blah blah get the distance to the mouse
			dx = FlxG.mouse.x - (x + width / 2);
			dy = FlxG.mouse.y - (y + height / 2);
			dist = Math.sqrt(dx * dx + dy * dy);
			if (dist < selectRadius)
			{
				// Select value adds up, and it adds up quicker if you're closer to the middle of the object.
				selectedValue = FlxU.bound(selectedValue + FlxU.bound((selectRadius - dist) / 3, 1, 20) * FlxG.elapsed * 0.1, 0.2, 1.0);
				alpha = 1.0;
			}
			else
			{
				selectedValue = FlxU.bound(selectedValue - 3 * FlxG.elapsed, 0, 0.8);
				alpha = 0.8;
			}
			
			// Cap the selected value based on if the area is clear.
			var c:uint = GameState.ref.hazy.unblurMask.getPixel32(x + width / 2, y + height / 2);
			selectedValue = Math.min(selectedValue, FlxU.getRGBA(c)[3] + 0.2);			
			
			// Start adding to the selected time when it's fully selected
			if (selectedValue >= 1.0)
			{
				selectedTime += FlxG.elapsed;
			}
			
			// Scale based on selectedness
			scale.x = scale.y = selectedValue * (maxScale-baseScale) + baseScale;
		}
		
	}

}